package usts.cs2020.classreportsystem.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import usts.cs2020.classreportsystem.Util.DateFormat;
import usts.cs2020.classreportsystem.Util.MD5Utils;
import usts.cs2020.classreportsystem.domain.*;
import usts.cs2020.classreportsystem.service.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private ReportService reportService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private AnswerService answerService;
    @Autowired
    private QToUService qToUService;

    @RequestMapping("/reg")
    public boolean register(@RequestBody User user) {
        user.setPassword(MD5Utils.getMD5Password(user.getPassword()));
        boolean save = userService.save(user);
        System.out.println(save);
        return save;
    }

    @RequestMapping("/get")
    public List<User> getAll() {
        List<User> userList = userService.list();
        return userList;
    }

    @RequestMapping("/login/{account}/{password}")
    public User login(@PathVariable("account") String account,@PathVariable("password") String password) {
        password = MD5Utils.getMD5Password(password);
        User login = userService.login(account, password);
        return login;
    }

    @RequestMapping("/uploadReport")
    public boolean uploadReport(@RequestParam("itemPhoto")MultipartFile itemPhoto,
                                @RequestParam("shoppingReceipts")MultipartFile shoppingReceipts,
                                @RequestParam("date")String date,
                                @RequestParam("cost")Double cost,
                                @RequestParam("acceptancePerson")String acceptancePerson,
                                @RequestParam("ownerClass")Integer ownerClass) throws FileNotFoundException {
        String itemPhotoPath = "/img/itemPhoto/"+ System.currentTimeMillis() + ".jpg";
        String shoppingReceiptsPath = "/img/shoppingReceipts/"+ System.currentTimeMillis() + ".jpg";
        File itemPhotoFile = new File("E:/javaEE/ClassReportSystem/src/main/resources/static"+itemPhotoPath);
        File shoppingReceiptsFile = new File("E:/javaEE/ClassReportSystem/src/main/resources/static"+shoppingReceiptsPath);
        try {
            itemPhoto.transferTo(itemPhotoFile);
            shoppingReceipts.transferTo(shoppingReceiptsFile);
            Report report = new Report();
            report.setCost(cost);
            report.setDate(DateFormat.strTodate(date));
            report.setOwnerClass(ownerClass);
            report.setAcceptancePerson(acceptancePerson);
            report.setItemPhoto(itemPhotoPath);
            report.setShoppingReceipts(shoppingReceiptsPath);
            //存入数据库
            reportService.save(report);
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
        return true;
    }

    @RequestMapping("/getReports/{classCode}")
    public List<Report> getReports(@PathVariable("classCode") Integer classCode) {
        LambdaQueryWrapper<Report> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Report::getOwnerClass,classCode);
        List<Report> list = reportService.list(lqw);
        return list;
    }

    @RequestMapping("getReport/{id}")
    public Report getReport(@PathVariable("id") Long id) {
        Report report = reportService.getById(id);
        return report;
    }

    //获取一个班级的人数
    @RequestMapping("getClassNumber/{classCode}")
    public long getClassNumber(@PathVariable("classCode") Integer classCode) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getClassCode,classCode);
        long count = userService.count(lqw);
        return count;
    }

    //提出一个质疑
    @RequestMapping("putQuestion")
    public boolean putQuestion(@RequestBody Question question) {
        boolean save = questionService.save(question);
        return save;
    }

    //查看确认人数
    @RequestMapping("checkQuestionNumber/{questionId}")
    public long checkQuestionNumber(@PathVariable("questionId")long questionId) {
        LambdaQueryWrapper<QToU> lqw = new LambdaQueryWrapper<>();
        lqw.eq(QToU::getQuestionId,questionId);
        long count = qToUService.count(lqw);
        return count;
    }

    //解答质疑
    @RequestMapping("answerQuestion")
    public boolean answerQuestion(@RequestBody Answer answer) {
        boolean save = answerService.save(answer);
        return save;
    }

    //查找所有质疑
    @RequestMapping("findAllQuestion/{reportId}")
    public List<Question> findAllQuestion(@PathVariable("reportId")Long reportId) {
        LambdaQueryWrapper<Question> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Question::getReportId,reportId);
        List<Question> list = questionService.list(lqw);
        return list;
    }

    //发表一个回答
    @RequestMapping("submitAnswer")
    public boolean submitAnswer(@RequestBody Answer answer) {
        boolean save = answerService.save(answer);
        return save;
    }

    //查看一个质疑的所有回答
    @RequestMapping("seeAnswer/{questionId}")
    public List<Answer> seeAnswer(@PathVariable("questionId")Long questionId) {
        LambdaQueryWrapper<Answer> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Answer::getQuestionId,questionId);
        List<Answer> list = answerService.list(lqw);
        return list;
    }

    //判断该用户是否确认过
    @RequestMapping("checkPersonConfirm")
    public boolean checkPersonConfirm(@RequestBody QToU q) {
        LambdaQueryWrapper<QToU> lqw = new LambdaQueryWrapper<>();
        lqw.eq(QToU::getQuestionId,q.getQuestionId()).eq(QToU::getUserId,q.getUserId());
        QToU one = qToUService.getOne(lqw);
        if (one!=null) {
            return true;
        }
        return false;
    }

    //确认质疑
    @RequestMapping("confirmAnswer")
    public boolean confirmAnswer(@RequestBody QToU q) {
        boolean save = qToUService.save(q);
        return save;
    }

    @RequestMapping("changeReportStatus/{reportId}")
    public boolean changeReportStatus(@PathVariable("reportId") Long reportId) {
        LambdaUpdateWrapper<Report> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(Report::getId,reportId).set(Report::getIsOver,1);
        boolean update = reportService.update(lqw);
        return update;
    }







}
